package dao.iml;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Course;
import pojo.Course_Lesson;
import pojo.Course_Section;
import utils.DateUtils;
import utils.DruidUtils;

import javax.swing.*;
import java.sql.SQLException;
import java.util.List;

public class CourseContentDaoImpl implements CourseContentDao {
    @Override
    public List<Course_Section> findSectionCourseId(int courseId) {
        try {
            //根据course_id获取到章节信息
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select * from course_section where course_id = ?";
            //获取到课时的章节信息
            List<Course_Section> sectionList = qr.query(sql, new BeanListHandler<Course_Section>(Course_Section.class), courseId);
            for (Course_Section course_section : sectionList) {
                List<Course_Lesson> lessonByCourseId = findLessonBysectionId(course_section.getId());
                course_section.setList(lessonByCourseId);
            }
            return sectionList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }


    }

    @Override
    public List<Course_Lesson> findLessonBysectionId(int sectionId) {
        try {
            //根据章节ID查询课时信息
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select * from course_lesson where section_id = ?";
            List<Course_Lesson> courseLessonList = qr.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class), sectionId);
            return courseLessonList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Course findCourseById(int course_id) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select * from course where id = ?";
            Course course = qr.query(sql, new BeanHandler<Course>(Course.class), course_id);
            return course;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Course findCourseByCourseId(int course_id) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select * from course where id = ?";
            Course course = qr.query(sql, new BeanHandler<Course>(Course.class), course_id);
            return course;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int saveSection(Course_Section cs) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "insert into course_section(id,course_id,section_name,description,order_num,create_time,update_time)" +
                    " values(?,?,?,?,?,?,?)";
            Object[] params = {cs.getId(),cs.getCourse_id(),cs.getSection_name(),cs.getDescription(),cs.getOrder_num(), DateUtils.getDateFormart(),DateUtils.getDateFormart()};
            int row = qr.update(sql, params);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Course_Section updateSection(Course_Section cs) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "update course set course_section " +
                    "course_id = ?,section_name = ?,description = ?,order_num = ?,create_time = ?,update_time = ? " +
                    "where id = ?";
            Object[] params = {cs.getCourse_id(),cs.getSection_name(),cs.getDescription(),cs.getOrder_num(),DateUtils.getDateFormart(),DateUtils.getDateFormart()};
            Course_Section course_section = qr.query(sql, new BeanHandler<Course_Section>(Course_Section.class), params);
            return course_section;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateSectionStatus(Course_Section course_section) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "update course_section set status = ? where id = ?";
            int row = qr.update(sql, course_section.getStatus(), course_section.getId());
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int saveLesson(Course_Lesson cs) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "INSERT INTO course_lesson(course_id,section_id,theme,duration," +
                    "is_free,order_num) values(?,?,?,?,?,?)";
            Object[] objects = {cs.getCourse_id(),cs.getSection_id(),cs.getTheme(),cs.getDuration(),cs.getIs_free(),cs.getOrderNum()
            };
            //返回受影响行数！
            int row = qr.update(sql, objects);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateLess(Course_Lesson cl) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course_lesson set course_id = ?,section_id = ?,theme = ?,duration = ?,is_free = ?,order_num = ?" +
                    " where id = ?";
            Object[] objects = {cl.getCourse_id(),cl.getSection_id(),cl.getTheme(),cl.getDuration(),cl.getIs_free(),cl.getOrderNum(),cl.getId()};
            int row = qr.update(sql,objects);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }


}
